﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Text;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{

    public class SummaryData : BaseFunction
    {


        public SummaryData()
            : base()
        {

        }

        public SummaryData(Dictionary<string, object> DicGlobalPara)
            : base(DicGlobalPara)
        {
        }

        public DataSet GetABFOLGE(DataSet dsTarget, DataRow dwABPOS)
        {
           
            string TableName = "ABFOLGE";
            try
            {
                orlReadCommand = OracleDB.CreateCommand();
                orlReadCommand.CommandText = "SELECT distinct ABFAUFKRS,ABFAUFLFD,ABFAUFPOS,trim(LSNAME1) as LSNAME1 "
                    + "  FROM ABFOLGE , LIEFDAT WHERE  ABFMANDANT = LSMANDANT " +
                    " AND ABFLIEFNR = LSLIEFNR AND TRIM(ABFLIEFNR) IS NOT NULL AND  " +
                     " ABFAUFKRS = :ABFAUFKRS AND ABFAUFLFD=:ABFAUFLFD AND ABFAUFPOS=:ABFAUFPOS";
                orlReadCommand.Parameters.Add("ABFAUFKRS", OracleType.NVarChar).Value = dwABPOS["ABPAUFKRS"].ToString();
                orlReadCommand.Parameters.Add("ABFAUFLFD", OracleType.NVarChar).Value = dwABPOS["ABPAUFLFD"].ToString();
                orlReadCommand.Parameters.Add("ABFAUFPOS", OracleType.NVarChar).Value = dwABPOS["ABPAUFPOS"].ToString();
                dsResult = Connect2Proxy(orlReadCommand);
                if (dsResult.Tables.Count > 0)
                {
                    dsResult.Tables[0].TableName = TableName;
                    dsResult.AcceptChanges();
                }
                 byte[] b;
                string value;

                foreach (DataRow rowABFOLGE in dsResult.Tables[TableName].Rows)
                {
                    
                    foreach (DataColumn dc in dsResult.Tables[TableName].Columns)
                    {
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(rowABFOLGE[dc.ColumnName].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        rowABFOLGE[dc.ColumnName] = value;

                    }
                    if (true)
                    {
                        dwABPOS["FACTORY"] = rowABFOLGE["LSNAME1"];
                    }
                }
                foreach (DataTable dtTemp in dsResult.Tables)
                {
                    if (dsTarget.Tables.Contains(TableName))
                    {
                        dsTarget.Tables[TableName].Clear();
                    }
                    dsTarget = ImportDataTable(dsTarget, dtTemp);
                }
                dsTarget.AcceptChanges();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex); 
            }
            return dsTarget;
        }
   
        public DataSet GetABAUFM(DataSet dsTarget, DataRow dwABPOS)
        {
           
            string TableName = "ABAUFM";
            try
            {
                orlReadCommand = OracleDB.CreateCommand();
                orlReadCommand.CommandText = "select a.abaaufkrs,a.abaauflfd,a.abaaufpos,b.abpaufkrs,b.abpauflfd,b.abpaufpos, "
                                            + " b.abpwakz,b.ABPART,b.ABPVAR,b.ABPFARBE,'' as USAGE,'' as FABRIC "
                                            +" from abaufm a,abpos b where a.abamandant = b.abpmandant "
                                            +" AND ABAVKAUFKRS=ABPAUFKRS AND ABAVKAUFLFD=ABPAUFLFD AND ABAVKAUFPOS=ABPAUFPOS "
                                            +" and ABAAPOS>'0001' "
                                            + "AND  ABAAUFKRS = :ABAAUFKRS AND ABAAUFLFD=:ABAAUFLFD AND ABPAUFPOS=:ABAAUFPOS";
                orlReadCommand.Parameters.Add("ABAAUFKRS", OracleType.NVarChar).Value = dwABPOS["ABPAUFKRS"].ToString();
                orlReadCommand.Parameters.Add("ABAAUFLFD", OracleType.NVarChar).Value = dwABPOS["ABPAUFLFD"].ToString();
                orlReadCommand.Parameters.Add("ABAAUFPOS", OracleType.NVarChar).Value = dwABPOS["ABPAUFPOS"].ToString();
                dsResult = Connect2Proxy(orlReadCommand);
                if (dsResult.Tables.Count > 0)
                {
                    dsResult.Tables[0].TableName = TableName;
                
               /* foreach (DataRow rowABAUFM in dsResult.Tables[TableName].Rows)
                {
                    dwABPOS["FABRIC"] = rowABAUFM["ABPART"].ToString().Trim() + "-"
                                    + rowABAUFM["ABPFARBE"].ToString().Trim() + "-"
                                    + rowABAUFM["ABPVAR"].ToString().Trim();
                }*/
                
                #region FOUND
                string usage ="";
                string fabric = "";

                foreach (DataRow rowTemp in dsResult.Tables["ABAUFM"].Rows)
                {
                    if (rowTemp["ABPWAKZ"].ToString() == "R")
                    {
                        usage = "纬纱";
                        fabric = rowTemp["ABPART"].ToString().Trim() + "-"
                                    + rowTemp["ABPFARBE"].ToString().Trim() + "-"
                                    + rowTemp["ABPVAR"].ToString().Trim();
                    }
                    else
                    {
                        if (rowTemp["ABPWAKZ"].ToString() == "K")
                        {
                            orlReadCommand = OracleDB.CreateCommand();
                            orlReadCommand.CommandText = "select a.abaaufkrs,a.abaauflfd,a.abaaufpos,b.abpaufkrs,b.abpauflfd,b.abpaufpos, "
                                                        + " b.abpwakz,b.ABPART,b.ABPVAR,b.ABPFARBE,'' as usage,'' as fabric "
                                                        + " from abaufm a,abpos b where a.abamandant = b.abpmandant "
                                                        + " AND ABAVKAUFKRS=ABPAUFKRS AND ABAVKAUFLFD=ABPAUFLFD AND ABAVKAUFPOS=ABPAUFPOS "
                                                        + " and ABAAPOS>'0001' "
                                                        + "AND  ABAAUFKRS = :ABAAUFKRS AND ABAAUFLFD=:ABAAUFLFD AND ABPAUFPOS=:ABAAUFPOS";
                            orlReadCommand.Parameters.Add("ABAAUFKRS", OracleType.NVarChar).Value = rowTemp["ABPAUFKRS"].ToString();
                            orlReadCommand.Parameters.Add("ABAAUFLFD", OracleType.NVarChar).Value = rowTemp["ABPAUFLFD"].ToString();
                            orlReadCommand.Parameters.Add("ABAAUFPOS", OracleType.NVarChar).Value = rowTemp["ABPAUFPOS"].ToString();
                            dsResult = Connect2Proxy(orlReadCommand);
                            dsResult = new DataSet();
                            if (dsResult.Tables.Count > 0)
                            {
                                foreach (DataRow rowTemp1 in dsResult.Tables[0].Rows)
                                {
                                    usage = "经纱";
                                    fabric = rowTemp1["ABPART"].ToString().Trim() + "-"
                                    + rowTemp1["ABPFARBE"].ToString().Trim() + "-"
                                    + rowTemp1["ABPVAR"].ToString().Trim();
                                }
                            }
                            else
                            {
                                usage = "";
                                fabric = "";
                            }

                        }
                        else
                        {
                            if (rowTemp["ABPWAKZ"].ToString() == "G")
                            {
                                orlReadCommand = OracleDB.CreateCommand();
                                orlReadCommand.CommandText = "select a.abaaufkrs,a.abaauflfd,a.abaaufpos,b.abpaufkrs,b.abpauflfd,b.abpaufpos, "
                                                            + " b.abpwakz,b.ABPART,b.ABPVAR,b.ABPFARBE,'' as usage,'' as fabric "
                                                            + " from abaufm a,abpos b where a.abamandant = b.abpmandant "
                                                            + " AND ABAVKAUFKRS=ABPAUFKRS AND ABAVKAUFLFD=ABPAUFLFD AND ABAVKAUFPOS=ABPAUFPOS "
                                                            + " and ABAAPOS>'0001' "
                                                            + "AND  ABAAUFKRS = :ABAAUFKRS AND ABAAUFLFD=:ABAAUFLFD AND ABPAUFPOS=:ABAAUFPOS";
                                orlReadCommand.Parameters.Add("ABAAUFKRS", OracleType.NVarChar).Value = rowTemp["ABPAUFKRS"].ToString();
                                orlReadCommand.Parameters.Add("ABAAUFLFD", OracleType.NVarChar).Value = rowTemp["ABPAUFLFD"].ToString();
                                orlReadCommand.Parameters.Add("ABAAUFPOS", OracleType.NVarChar).Value = rowTemp["ABPAUFPOS"].ToString();
                                dsResult = Connect2Proxy(orlReadCommand);
                                dsResult = new DataSet();
                                if (dsResult.Tables.Count > 0)
                                {
                                    foreach (DataRow rowTemp1 in dsResult.Tables[0].Rows)
                                    {
                                        if (rowTemp1["ABPWAKZ"].ToString() == "R")
                                        {
                                            usage = "纬纱";
                                            fabric = rowTemp1["ABPART"].ToString().Trim() + "-"
                                                        + rowTemp1["ABPFARBE"].ToString().Trim() + "-"
                                                        + rowTemp1["ABPVAR"].ToString().Trim();
                                        }
                                        else
                                        {
                                            if (rowTemp1["ABPWAKZ"].ToString() == "K")
                                            {
                                                orlReadCommand = OracleDB.CreateCommand();
                                                orlReadCommand.CommandText = "select a.abaaufkrs,a.abaauflfd,a.abaaufpos,b.abpaufkrs,b.abpauflfd,b.abpaufpos, "
                                                                            + " b.abpwakz,b.ABPART,b.ABPVAR,b.ABPFARBE,'' as usage,'' as fabric "
                                                                            + " from abaufm a,abpos b where a.abamandant = b.abpmandant "
                                                                            + " AND ABAVKAUFKRS=ABPAUFKRS AND ABAVKAUFLFD=ABPAUFLFD AND ABAVKAUFPOS=ABPAUFPOS "
                                                                            + " and ABAAPOS>'0001' "
                                                                            + "AND  ABAAUFKRS = :ABAAUFKRS AND ABAAUFLFD=:ABAAUFLFD AND ABPAUFPOS=:ABAAUFPOS";
                                                orlReadCommand.Parameters.Add("ABAAUFKRS", OracleType.NVarChar).Value = rowTemp1["ABPAUFKRS"].ToString();
                                                orlReadCommand.Parameters.Add("ABAAUFLFD", OracleType.NVarChar).Value = rowTemp1["ABPAUFLFD"].ToString();
                                                orlReadCommand.Parameters.Add("ABAAUFPOS", OracleType.NVarChar).Value = rowTemp1["ABPAUFPOS"].ToString();
                                                dsResult = Connect2Proxy(orlReadCommand);
                                                dsResult = new DataSet();
                                                if (dsResult.Tables.Count > 0)
                                                {
                                                    foreach (DataRow rowTemp2 in dsResult.Tables[0].Rows)
                                                    {
                                                        usage = "经纱";
                                                        fabric = rowTemp2["ABPART"].ToString().Trim() + "-"
                                                        + rowTemp2["ABPFARBE"].ToString().Trim() + "-"
                                                        + rowTemp2["ABPVAR"].ToString().Trim();
                                                    }
                                                }
                                                else
                                                {
                                                    usage = "";
                                                    fabric = "";
                                                }
                                            }
                                        }

                                    }
                                }
                                else
                                {
                                    usage = "";
                                    fabric = "";
                                }
                            }
                            else
                            {
                                usage = "";
                                fabric = "";
                            }

                        }
                    }
                    /*
                                    DataTable tableABAUFM=dsTarget.Tables["ABAUFM"];
                                    string expression = "(ABPWAKZ='R' or ABPWAKZ='K')";
                                    DataRow[] rowFound;
                                    rowFound = tableABAUFM.Select(expression);
                                    string nextABPAUFKRS,nextABPAUFLFD,nextABPAUFPOS;
                                    bool flgFound = false;
                
                                    while (!flgFound)
                                    {
                                        usage="";
                                        #region Found
                                        if (rowFound.Count() > 0)
                                        {
                                            flgFound = true;
                                            foreach(DataRow rowTemp in rowFound)
                                            {
                                                if(rowTemp["ABPWAKZ"].ToString()=="R")
                                                {
                                                    if (!usage.Contains("W纱;"))
                                                    {
                                                        usage += "W纱;";
                                                    }
                                                    dwABPOS["FABRIC"] = rowTemp["ABPART"].ToString().Trim()+"-"
                                                        + rowTemp["ABPFARBE"].ToString().Trim() + "-"
                                                        + rowTemp["ABPVAR"].ToString().Trim();
                                                }
                                               if(rowTemp["ABPWAKZ"].ToString()=="K")
                                                {
                                                    if (!usage.Contains("J纱;"))
                                                    {
                                                        usage += "J纱;";
                                                    }
                                                    orlReadCommand.Parameters["ABAAUFKRS"].Value = rowTemp["ABPAUFKRS"].ToString();
                                                    orlReadCommand.Parameters["ABAAUFLFD"].Value = rowTemp["ABPAUFLFD"].ToString();
                                                    orlReadCommand.Parameters["ABAAUFPOS"].Value = rowTemp["ABPAUFPOS"].ToString();
                                                    dsResult = new DataSet();
                                                    dsResult = Connect2Proxy(orlReadCommand);
                                                    if (dsResult.Tables.Count > 0)
                                                    {
                                                        dsResult.Tables[0].TableName = TableName;
                                                    }
                                                    DataRow rowAB55;
                                                   if(dsResult.Tables[0].Rows.Count>0)
                                                   {
                                                       rowAB55 = dsResult.Tables[0].Rows[0];
                                                       dwABPOS["FABRIC"] = rowAB55["ABPART"].ToString().Trim() + "-"
                                                           + rowTemp["ABPFARBE"].ToString().Trim() + "-"
                                                           + rowTemp["ABPVAR"].ToString().Trim();
                                                   }

                                                }
                                            }
                                            dwABPOS["USAGE"] = usage;

                                        }
                                    }

                                        #endregion
                                        else
                                        {
                                            if (tableABAUFM.Rows.Count == 0)
                                            {
                                                flgFound = true;
                                                break;
                                            }
                                            else
                                            {
                                                foreach (DataRow rowTemp in tableABAUFM.Rows)
                                                {
                                                    orlReadCommand.Parameters["ABAAUFKRS"].Value = rowTemp["ABPAUFKRS"].ToString();
                                                    orlReadCommand.Parameters["ABAAUFLFD"].Value = rowTemp["ABPAUFLFD"].ToString();
                                                    orlReadCommand.Parameters["ABAAUFPOS"].Value = rowTemp["ABPAUFPOS"].ToString();
                                                    dsResult = new DataSet();
                                                    dsResult = Connect2Proxy(orlReadCommand);
                                                    if (dsResult.Tables.Count > 0)
                                                    {
                                                        dsResult.Tables[0].TableName = TableName;
                                                    }
                                                    dsTarget.Merge(dsResult);
                                                    tableABAUFM = dsTarget.Tables["ABAUFM"];
                                                    rowFound = tableABAUFM.Select(expression);
                                                }
                                            }
                                        }
                                    }
                                    */
                  rowTemp["USAGE"] = usage;
                  rowTemp["FABRIC"] = fabric;
                }
                    dwABPOS["USAGE"] = usage;
                    dwABPOS["FABRIC"] = fabric;
                    
                }
                #endregion
           

                foreach (DataTable dtTemp in dsResult.Tables)
                {
                    if (dsTarget.Tables.Contains(TableName))
                    {
                        dsTarget.Tables[TableName].Clear();
                    }
                    dsTarget = ImportDataTable(dsTarget, dtTemp);
                }
                dsTarget.AcceptChanges();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex); 
            }
            return dsTarget;
            
        }
        
        
        /*public DataSet BuildAB50Data(DataSet dsTarget, DataRow dwABPOS)
        {
            try
            {
                dsTarget = GetABKOPF(dsTarget, dwABPOS);
                dsTarget = GetABPOS(dsTarget, dwABPOS);
                //dsTarget = GetABAUFM(dsTarget, dwABPOS);
                //dsTarget = GetGAAUF(dsTarget, dwABPOS);
                //dsTarget = GetABFOLGE(dsTarget, dwABPOS);
                //dsTarget = GetGSDAT(dsTarget, dwABPOS);
                
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex); 
            }
            return dsTarget;
        }*/
    }
}